﻿using System;
using System.Windows.Forms;

namespace HartConfigTool
{
    public partial class funcControls : UserControl
    {
        private ITEM seledItem;
        public event EventHandler transInfoEventHandler;
        public event EventHandler switchPanelEventHandler;
        enum ITEM
        {
            eNone = 0,
            eInstrumentInformation = 1,
            eInstrumentConfiguration = 2,
            eProcessMonitoring = 3,
            eInstrumentFineAdj = 4,
            eInstrumentCalibration = 5,
            eInstrumentMaintenance = 6,
            eUserCalibration = 7,
            eManufacturerCalibration = 8,
            eAdvanced = 9,
        };
        public funcControls()
        {
            InitializeComponent();

            flowLayoutPanel1.SizeChanged += (s, e) =>
            {
                var width = flowLayoutPanel1.ClientSize.Width - flowLayoutPanel1.Margin.Horizontal;
                btnInstruInfo.Width = width;
                btnInstruConfig.Width = width;
                btnProcMonitor.Width = width;
                btnInstruMicroAdj.Width = width;
                btnInstruCali.Width = width;
                btnInstruMainten.Width = width;
                btnUserCali.Width = width;
                btnManufacCali.Width = width;
                btnAdvanced.Width = width;

                width = flowLayoutPanel1.ClientSize.Width - flowLayoutPanel1.Margin.Horizontal - 40;

                // Instrument information
                btnInstruInfoInstruInfo.Width = width;
                btnInstruInfoAddrSet.Width = width;
                btnInstruInfoTransInfo.Width = width;
                btnInstruInfoMoreInfo.Width = width;
                // Instrument Configuration
                btnInstruConfigRgeSet.Width = width;
                btnInstruConfigSrcMove.Width = width;
                btnInstruConfigOutputProp.Width = width;
                btnInstruConfigFailProt.Width = width;
                // Process Monitoring
                btnProcMonitorProcMonitor.Width = width;
                // Instrument fine adjustment
                btnInstruMicroAdjCurrent.Width = width;
                btnInstruMicroAdjCaliUpDown.Width = width;
                btnInstruMicroAdjZero.Width = width;
                btnInstruMicroAdjCancel.Width = width;
                // Instrument calibration
                btnInstruCaliAccuracyTest.Width = width;
                btnInstruCaliRecordConfig.Width = width;
                // Instrument maintenance
                btnInstruMaintenMatInfo.Width = width;
                btnInstruMaintenSelfTestInfo.Width = width;
                btnInstruMaintenKeyTest.Width = width;
                // User Calibration
                btnUserCalibration.Width = width;
                btnUserCaliInstructions.Width = width;
                // Manufacturer Calibration
                btnManufacCaliLinearization.Width = width;
                btnManufacCaliTempCompensation.Width = width;
                btnManufacCaliInstructions.Width = width;
                // Advanced
                btnAdvancedSensorConfig.Width = width;
                btnAdvancedAdditions.Width = width;
            };

            seledItem = ITEM.eNone;
        }
        public void InstrumentInforSubMenuDisp(bool bFlg)
        {
            btnInstruInfoInstruInfo.Visible = bFlg;
            btnInstruInfoAddrSet.Visible = bFlg;
            btnInstruInfoTransInfo.Visible = bFlg;
            btnInstruInfoMoreInfo.Visible = bFlg;
        }
        public void InstrumentConfigSubMenuDisp(bool bFlg)
        {
            btnInstruConfigRgeSet.Visible = bFlg;
            btnInstruConfigSrcMove.Visible = bFlg;
            btnInstruConfigOutputProp.Visible = bFlg;
            btnInstruConfigFailProt.Visible = bFlg;
        }
        public void ProcessMonitoringSubMenuDisp(bool bFlg)
        {
            btnProcMonitorProcMonitor.Visible = bFlg;
        }
        public void InstrumentFineAdjSubMenuDisp(bool bFlg)
        {
            btnInstruMicroAdjCurrent.Visible = bFlg;
            btnInstruMicroAdjCaliUpDown.Visible = bFlg;
            btnInstruMicroAdjZero.Visible = bFlg;
            btnInstruMicroAdjCancel.Visible = bFlg;
        }
        public void InstrumentCaliSubMenuDisp(bool bFlg)
        {
            btnInstruCaliAccuracyTest.Visible = bFlg;
            btnInstruCaliRecordConfig.Visible = bFlg;
        }
        public void InstrumentMaintenanceSubMenuDisp(bool bFlg)
        {
            btnInstruMaintenMatInfo.Visible = bFlg;
            btnInstruMaintenSelfTestInfo.Visible = bFlg;
            btnInstruMaintenKeyTest.Visible = bFlg;
        }
        public void UserCalibrationSubMenuDisp(bool bFlg)
        {
            btnUserCalibration.Visible = bFlg;
            btnUserCaliInstructions.Visible = bFlg;
        }
        public void ManufacturerCalibrationSubMenuDisp(bool bFlg)
        {
            btnManufacCaliLinearization.Visible = bFlg;
            btnManufacCaliTempCompensation.Visible = bFlg;
            btnManufacCaliInstructions.Visible = bFlg;
        }
        public void AdvancedSubMenuDisp(bool bFlg)
        {
            btnAdvancedSensorConfig.Visible = bFlg;
            btnAdvancedAdditions.Visible = bFlg;
        }
        private void btnInstruInfo_Click(object sender, EventArgs e)
        {
            UpdateFuncSelComponent(ITEM.eInstrumentInformation);
            transInfoEventHandler?.Invoke(this.btnInstruInfo, EventArgs.Empty);
            switchPanelEventHandler?.Invoke(this.btnInstruInfo, EventArgs.Empty);
        }

        private void btnInstruConfig_Click(object sender, EventArgs e)
        {
            UpdateFuncSelComponent(ITEM.eInstrumentConfiguration);
        }

        private void btnProcMonitor_Click(object sender, EventArgs e)
        {
            UpdateFuncSelComponent(ITEM.eProcessMonitoring);
        }

        private void btnInstruMicroAdj_Click(object sender, EventArgs e)
        {
            UpdateFuncSelComponent(ITEM.eInstrumentFineAdj);
        }

        private void btnInstruCali_Click(object sender, EventArgs e)
        {
            UpdateFuncSelComponent(ITEM.eInstrumentCalibration);
        }

        private void btnInstruMainten_Click(object sender, EventArgs e)
        {
            UpdateFuncSelComponent(ITEM.eInstrumentMaintenance);
        }

        private void btnUserCali_Click(object sender, EventArgs e)
        {
            UpdateFuncSelComponent(ITEM.eUserCalibration);
        }

        private void btnManufacCali_Click(object sender, EventArgs e)
        {
            UpdateFuncSelComponent(ITEM.eManufacturerCalibration);
        }

        private void btnAdvanced_Click(object sender, EventArgs e)
        {
            UpdateFuncSelComponent(ITEM.eAdvanced);
        }
        private void UpdateFuncSelComponent(ITEM currentItem)
        {
            if (this.seledItem == currentItem)
            {
                return;
            }

            if (this.seledItem != ITEM.eNone)
            {
                CloseItem(this.seledItem);
            }

            this.seledItem = currentItem;

            switch (currentItem)
            {
                case ITEM.eInstrumentInformation:
                    InstrumentInforSubMenuDisp(true);
                    break;
                case ITEM.eInstrumentConfiguration:
                    InstrumentConfigSubMenuDisp(true);
                    break;
                case ITEM.eProcessMonitoring:
                    ProcessMonitoringSubMenuDisp(true);
                    break;
                case ITEM.eInstrumentFineAdj:
                    InstrumentFineAdjSubMenuDisp(true);
                    break;
                case ITEM.eInstrumentCalibration:
                    InstrumentCaliSubMenuDisp(true);
                    break;
                case ITEM.eInstrumentMaintenance:
                    InstrumentMaintenanceSubMenuDisp(true);
                    break;
                case ITEM.eUserCalibration:
                    UserCalibrationSubMenuDisp(true);
                    break;
                case ITEM.eManufacturerCalibration:
                    ManufacturerCalibrationSubMenuDisp(true);
                    break;
                case ITEM.eAdvanced:
                    AdvancedSubMenuDisp(true);
                    break;
                default:
                    break;
            }
        }
        private void CloseItem(ITEM item)
        {
            switch (item)
            {
                case ITEM.eInstrumentInformation:
                    InstrumentInforSubMenuDisp(false);
                    break;
                case ITEM.eInstrumentConfiguration:
                    InstrumentConfigSubMenuDisp(false);
                    break;
                case ITEM.eProcessMonitoring:
                    ProcessMonitoringSubMenuDisp(false);
                    break;
                case ITEM.eInstrumentFineAdj:
                    InstrumentFineAdjSubMenuDisp(false);
                    break;
                case ITEM.eInstrumentCalibration:
                    InstrumentCaliSubMenuDisp(false);
                    break;
                case ITEM.eInstrumentMaintenance:
                    InstrumentMaintenanceSubMenuDisp(false);
                    break;
                case ITEM.eUserCalibration:
                    UserCalibrationSubMenuDisp(false);
                    break;
                case ITEM.eManufacturerCalibration:
                    ManufacturerCalibrationSubMenuDisp(false);
                    break;
                case ITEM.eAdvanced:
                    AdvancedSubMenuDisp(false);
                    break;
                default:
                    break;
            }
        }

        private void btnInstruInfoTransInfo_Click(object sender, EventArgs e)
        {
            switchPanelEventHandler?.Invoke(this.btnInstruInfoTransInfo, EventArgs.Empty);
        }

        private void btnInstruInfoAddrSet_Click(object sender, EventArgs e)
        {
            switchPanelEventHandler?.Invoke(this.btnInstruInfoAddrSet, EventArgs.Empty);
        }
    }
}
